package juc;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author fangkun
 * @create 2022-12-09-15:39
 */

/**
 * ThreadLocal可以确保每个线程都可以得到各自单独的一个SimpleDateFormat的对象，那么自然也就不存在竞争问题了。
 */
public class DateUtils2 {

    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static String format(LocalDateTime localDateTime){
        return DATE_TIME_FORMATTER.format(localDateTime);
    }

    public static LocalDateTime parse(String dateString){
        return LocalDateTime.parse(dateString,DATE_TIME_FORMATTER);
    }

    public static void main(String[] args) {
        for (int i = 1; i <= 30; i++) {
            new Thread(() -> {
                    System.out.println(DateUtils2.parse("2022-12-12 11:11:11"));
            },String.valueOf(i)).start();
        }
    }
}
